package com.xuecheng.learning.service.impl;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.base.utils.StringUtil;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.mapper.XcChooseCourseMapper;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.model.po.XcChooseCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学习中心实现类
 * @author 冯佳鑫 又菜又爱学
 * @version 1.0
 * @DATE now()
 */
@Service
//学习资格状态 [{"code":"702001","desc":"正常学习"},{"code":"702002","desc":"没有选课或选课后没有支付"},{"code":"702003","desc":"已过期需要申请续期或重新支付"}]
public class LearningServiceimpl implements com.xuecheng.learning.service.LearningService {
    @Autowired
    MyCourseTablesServiceimpl myCourseTablesServiceimpl;
    @Autowired
    XcChooseCourseMapper xcChooseCourseMapper;
    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    MediaServiceClient mediaServiceClient;
    @Override
    public RestResponse<String> getvideo(String user_id,Long courseId, Long teachplanId, String mediaId) {
//拿到课程发布信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish==null){
            return RestResponse.validfail("该课程不存在");
        }
        //判断是否登陆
        //用户已登陆======
        if (StringUtil.isNotEmpty(user_id)){
            //拿到该该用户对此课程的学习资格======
            XcCourseTablesDto learningStatus = myCourseTablesServiceimpl.getLearningStatus(user_id, courseId);
            //如果学习资格为正常，则可以获取视频
            if (learningStatus.learnStatus.equals("702001")){
                //获有学习资格了远程调用媒资服务查询视频的播放地址======
                RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                return playUrlByMediaId;
            }
            else if (learningStatus.learnStatus.equals("702002")){
                return RestResponse.validfail("无法播放学习视频，没有选课或选课后没有支付");
            }
            else {
                return RestResponse.validfail("已过期需要申请续期或重新支付");
            }
        }
        //用户未登陆======

        //拿到该该用户对此课程的学习资格======

        //查询是否免费
        //免费
        if (coursepublish.getCharge().equals("201000")){
            //获有学习资格了远程调用媒资服务查询视频的播放地址======
            RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
            return playUrlByMediaId;
        }
        //收费
            return RestResponse.validfail("该课程需要购买");




    }
}
